package com.zhongxiangspace.controller.pagemanage;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.zhongxiangspace.entity.Advert;
import com.zhongxiangspace.entity.AdvertSite;
import com.zhongxiangspace.entity.QueryAnalyzer;
import com.zhongxiangspace.result.ExceptionResult;
import com.zhongxiangspace.result.NormalResult;
import com.zhongxiangspace.result.Response;
import com.zhongxiangspace.result.Result;
import com.zhongxiangspace.service.AdvertService;
import com.zhongxiangspace.tools.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 广告
 */
@Controller
public class AdvertController {
    @Autowired
    private AdvertService advertService;

    /**
     * 所有的广告
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/advert/details/updata.do")
    public Result updata(Advert advert) {
        try {
            advert.setModify_time(DateUtil.getDateTime());
            boolean flag = this.advertService.updata(advert);
            return new NormalResult(flag);
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
    }

    /**
     * /**
     * 修改的广告
     *
     * @return
     */
    @GetMapping("/advert/details/edit.do")
    public String edit(Model model, Integer id) {
        List<AdvertSite> advertSite = this.advertService.queryAllAdvertSite();
        model.addAttribute("data", advertSite);
        Advert advert = this.advertService.queryOne(id);
        String gson = new Gson().toJson(advert);
        model.addAttribute("gson", gson);
        return "basics/advert/advert_updata";
    }

    /**
     * 异步删除 data
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/advert/details/delete.do")
    public Response delete(Integer id) {
        boolean flag = false;
        if (id != null) {
            flag = this.advertService.delete(id);
        }
        Response result = new Response(0, "success", 0, flag);
        return result;
    }


    /**
     * 异步修改上下线 data
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/advert/details/status.do")
    public Response status(Integer id) {
        boolean flag = false;
        if (id != null) {
            Advert advert = this.advertService.queryOne(id);
            advert.setModify_time(DateUtil.getDateTime());
            if (advert != null && advert.getShowCode().equals(0)) {
                advert.setShowCode(1);
                flag = this.advertService.updataStatusCode(advert);
            } else if (advert != null && advert.getShowCode().equals(1)) {
                advert.setShowCode(0);
                flag = this.advertService.updataStatusCode(advert);
            }
        }
        Response result = new Response(0, "", 0, flag);
        return result;
    }


    /**
     * 所有的广告
     *
     * @return
     */
    @GetMapping("/advert/details/list.do")
    public String list(Model model) {
        List<AdvertSite> advertSite = this.advertService.queryAllAdvertSite();
        model.addAttribute("data", advertSite);
        return "basics/advert/advert_list";
    }

    /**
     * 所有的广告
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/advert/details/ajaxList.do")
    public Response ajaxList(QueryAnalyzer queryAnalyzer) {
        try {
            PageHelper.startPage(queryAnalyzer.getPage(), queryAnalyzer.getLimit(), "modify_time  DESC");
            Page<Map<String, Object>> adverts = (Page<Map<String, Object>>) this.advertService.queryAdvert(queryAnalyzer);
            return new Response(0, "SUCCESS", adverts.getTotal(), adverts.getResult());
        } catch (Exception e) {
            return new Response(0, "ERROR", 0, e);
        }
    }

    /**
     * 添加广告
     *
     * @return
     */
    @GetMapping("/advert/details/add.do")
    public String add(Model model) {
        List<AdvertSite> advertSite = this.advertService.queryAllAdvertSite();
        model.addAttribute("data", advertSite);
        return "basics/advert/advert_add";
    }

    /**
     * 添加广告
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/advert/details/insert.do")
    public Result insert(Advert advert) {
        try {
            advert.setCreate_time(DateUtil.getDateTime());
            advert.setModify_time(DateUtil.getDateTime());
            boolean flag = this.advertService.insert(advert);
            return new NormalResult(flag);
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
    }
}
